import BButton from "../btoolkit/components/BButton";
import PopupShareTip from "./PopupShareTip";
import QuLe, { EventOnRemoteConfigLoaded } from "../btoolkit/sdk/QuLe";
import EventManager from "../btoolkit/EventManager";
import App from "../btoolkit/framework/App";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BtnShare5Times extends cc.Component {

    static instance: BtnShare5Times = null;

    private isAlignLeft = false;

    onDestroy() {
        BtnShare5Times.instance = null;
    }

    onLoad() {
        BtnShare5Times.instance = this;
        this.node.active = false;

        //判断是否开启分享5次的功能
        if (QuLe.instance.remoteConfig) {
            this.init();
        } else {
            EventManager.addEvent(EventOnRemoteConfigLoaded, () => {
                this.init();
            });
        }
    }

    start(){
        this.isAlignLeft = this.node.getComponent(cc.Widget).isAlignLeft;
        if (this.isAlignLeft) {
            this.setToLeft();
        } else {
            this.setToRight();
        }
    }

    setToLeft() {
        let widget: cc.Widget = this.node.getComponent(cc.Widget);
        widget.isAlignLeft = true;
        widget.isAlignRight = false;
        widget.left = App.instance.isLiuHaiScreen ? 72 : 12;
    }

    setToRight() {
        let widget: cc.Widget = this.node.getComponent(cc.Widget);
        widget.isAlignLeft = false;
        widget.isAlignRight = true;
        widget.right = App.instance.isLiuHaiScreen ? 72 : 12;
    }

    private init() {
        if (QuLe.instance.remoteConfig.share5times) {
            this.node.active = true;
            BButton.addButton(this.node).setTouchUpEvent(() => {
                PopupShareTip.show(null);
            });
        }
    }

}
